<?php

require_once 'lib/HTTP/Session2.php';

/**
 * @export SessionClass
 * @export-metadata singleinstance
 * @export-metadata static-factory:instance
 */
class SASession implements Session {

    function __construct() {
        HTTP_Session2::start();
    }
    
    private static $instance;

    public static function instance() {
        if(!self::$instance){
            self::$instance = new SASession();
        }
        
        return self::$instance;
    }

    public function &get($key, $default = null) {
        return HTTP_Session2::get($key, $default);
    }

    public function has($key) {
        return isset($_SESSION[$key]);
    }

    public function id() {
        return HTTP_Session2::detectID();
    }

    public function put($key, $value) {
        HTTP_Session2::set($key, $value);
    }

    public function remove($key) {
        $oldValue = $this->get($key);
        unset($_SESSION[$key]);
        return $oldValue;
    }

    public function state() {
        $state = SessionStates::ACTIVE;
        if (HTTP_Session2::isExpired()) {
            $state = SessionStates::EXPIRED;
        }

        return $state;
    }

    public function close() {
        HTTP_Session2::destroy();
    }

}

?>
